package com.yidu.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.yidu.action.vo.StatisticsVo;
import com.yidu.common.Tools;
import com.yidu.dao.ListenBranchSellDetailMapper;
import com.yidu.dao.ListenBranchWholesaleDateilMapper;
import com.yidu.domain.ListenBackstageUser;
import com.yidu.domain.ListenBranchWholesaleDateil;
import com.yidu.service.ListenBranchWholesaleDateilService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangyu
 * @since 2018-11-27
 */
@Service
public class ListenBranchWholesaleDateilServiceImpl  implements ListenBranchWholesaleDateilService {
	/*
	 * 使用批发明细导
	 */
	@Resource
	private ListenBranchWholesaleDateilMapper dateilMapper;
	
	@Resource
	private ListenBranchSellDetailMapper bsDao;
	/**
	 * 批发明细增加的方法【有则增加无则不增加】
	 */
	@Override
	public int add(ListenBranchWholesaleDateil dateil) {
		
		return dateilMapper.insertSelective(dateil);
	}
	/**
	 * 批发明细查询所有
	 */
	@Override
	public List<ListenBranchWholesaleDateil> selectAll(Map<String, Object>map) {
		
		return dateilMapper.detailSelectAll(map);
	}
	/**
	 * 批发明细查询所有行数的方法
	 */
	@Override
	public int selectCount(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return dateilMapper.selectCount(map);
	}
	@Override
	public List<StatisticsVo> wholesaleStatistics(Map<String, Object> map) {
		
		return dateilMapper.wholesaleStatistics(map);
	}
	@Override
	public List<StatisticsVo> selectMonthsNumberPriceStatistics(HttpSession session,String year,String attribute) {
		ListenBackstageUser users = (ListenBackstageUser) session.getAttribute("user");
		String month[]= {"01","02","03","04","05","06","07","08","09","10","11","12"};
		List<StatisticsVo> sList=new ArrayList<>();
		
			String value="";
			String blooean="1";
		
			Map<String, Object> map=new HashMap<>();
			map.put("companyId", users.getSite());
			if(!Tools.isEmpty(year)) {
				map.put("year", year);
			}
			List<StatisticsVo> list=dateilMapper.selectMonthsNumberPriceStatistics(map);
			List<StatisticsVo> list2=bsDao.selectMonthsNumberPriceStatistics(map);
			
			if("number".equals(attribute)) {
				for (int i = 0; i < month.length; i++) {
					for (StatisticsVo statisticsVo : list) {
					if(month[i].equals(statisticsVo.getMonths())) {
						value+=statisticsVo.getValue()+",";
						blooean="1";
						break;
					}else {
						blooean="2";
					}
				}
					
				if(blooean.equals("2")) {
					value+="0"+",";
				}
				
			}
			StatisticsVo sVo=new StatisticsVo();
			sVo.setName("批发乐器月季数量");
			sVo.setValue(value);
			
			sList.add(sVo);
			
			value="";
			String blooean2="1";
		
			 
				for (int i = 0; i < month.length; i++) {
					for (StatisticsVo statisticsVo : list2) {
					if(month[i].equals(statisticsVo.getMonths())) {
						value+=statisticsVo.getValue()+",";
						blooean2="1";
						break;
					}else {
						blooean2="2";
					}
				}
					
				if(blooean2.equals("2")) {
					value+="0"+",";
				}
				
			}
			StatisticsVo sVo2=new StatisticsVo();
			sVo2.setName("零售乐器月季数量");
			sVo2.setValue(value);
			sList.add(sVo2);
			}else if("price".equals(attribute)) {
				for (int i = 0; i < month.length; i++) {
					for (StatisticsVo statisticsVo : list) {
					if(month[i].equals(statisticsVo.getMonths())) {
						value+=statisticsVo.getPrice()+",";
						blooean="1";
						break;
					}else {
						blooean="2";
					}
				}
					
				if(blooean.equals("2")) {
					value+="0"+",";
				}
				
			}
			StatisticsVo sVo=new StatisticsVo();
			sVo.setName("批发乐器月季总额");
			sVo.setValue(value);
			
			sList.add(sVo);
			
			value="";
			String blooean2="1";
		
			 
				for (int i = 0; i < month.length; i++) {
					for (StatisticsVo statisticsVo : list2) {
					if(month[i].equals(statisticsVo.getMonths())) {
						value+=statisticsVo.getPrice()+",";
						blooean2="1";
						break;
					}else {
						blooean2="2";
					}
				}
					
				if(blooean2.equals("2")) {
					value+="0"+",";
				}
				
			}
			StatisticsVo sVo2=new StatisticsVo();
			sVo2.setName("零售乐器月季总额");
			sVo2.setValue(value);
			sList.add(sVo2);
			}
			
		return sList;
	}

}
